package com.xiye.common.session.redis.impl;

import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xiye.common.session.redis.RedisSessionDAO;

import org.springframework.beans.factory.annotation.Value;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author 西野
 * @version V1.0
 * @title RedisManager.java
 * @package com.xiye.common.session.redis.impl
 * @description redis实现
 * @date 2018-07-12
 */
@SuppressWarnings("deprecation")
public class RedisManager {
    private static Logger logger = LoggerFactory.getLogger(RedisSessionDAO.class);

    @Value("${redis.host}")
    private String host;// ip地址

    @Value("${redis.port}")
    private int port;// 端口号

    @Value("${redis.pass}")
    private String pass;// 密码

    @Value("${redis.timeout}")
    private int timeout;// 超时时间

    // 0 - never expire
    private int expire = 0;// 命令

    private static JedisPool jedisPool = null;

    public RedisManager() {
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:50:54
     * @Description 初始化方法
     * @Param []
     * @Return void
     */
    public void init() {
        /*Properties pros = getPprVue();
        host = pros.getProperty("redis.host"); // 地址
        port = Integer.valueOf(pros.getProperty("redis.port")); // 端口
        pass = pros.getProperty("redis.pass"); // 密码
        timeout = Integer.valueOf(pros.getProperty("redis.timeout")); // 超时时间*/
        if (null == host || 0 == port) {
            logger.error("请初始化redis配置文件！");
            throw new NullPointerException("找不到redis配置");
        }
        if (jedisPool == null) {
            // jedisPool = JedisUtil.getJedisPool();
            if (pass.isEmpty()) {
                jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
            } else {
                jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout, pass);
            }
        }
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:51:10
     * @Description get value from redis
     * @Param [key]
     * @Return byte[]
     */
    public byte[] get(byte[] key) {
        byte[] value = null;
        Jedis jedis = jedisPool.getResource();
        try {
            value = jedis.get(key);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:51:19
     * @Description get value from redis
     * @Param [key]
     * @Return java.lang.String
     */
    public String get(String key) {
        String value = null;
        Jedis jedis = jedisPool.getResource();
        try {
            value = jedis.get(key);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:51:28
     * @Description 存值
     * @Param [key, value]
     * @Return byte[]
     */
    public byte[] set(byte[] key, byte[] value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (this.expire != 0) {
                jedis.expire(key, this.expire);
            }
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:51:39
     * @Description 新增(存储List)
     * @Param [key, list]
     * @Return void
     */
    public void addList(String key, List<String> list) {
        Jedis jedis = jedisPool.getResource();
        jedis.del(key); // 开始前，先移除所有的内容
        for (String value : list) {
            jedis.rpush(key, value);
        }
        jedis.close();
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:51:51
     * @Description 获取List
     * @Param [key]
     * @Return java.util.List<java.lang.String>
     */
    public List<String> getList(String key) {
        Jedis jedis = jedisPool.getResource();
        List<String> list = jedis.lrange(key, 0, -1);
        jedis.close();
        return list;
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:52:10
     * @Description 存值
     * @Param [key, value]
     * @Return java.lang.String
     */
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (this.expire != 0) {
                jedis.expire(key, this.expire);
            }
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:52:24
     * @Description 存值
     * @Param [key, value, expire]
     * @Return byte[]
     */
    public byte[] set(byte[] key, byte[] value, int expire) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:52:33
     * @Description 存值
     * @Param [key, value, expire]
     * @Return java.lang.String
     */
    public String set(String key, String value, int expire) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(key, value);
            if (expire != 0) {
                jedis.expire(key, expire);
            }
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:52:41
     * @Description 删除
     * @Param [key]
     * @Return void
     */
    public void del(byte[] key) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.del(key);
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:52:56
     * @Description 删除
     * @Param [key]
     * @Return void
     */
    public void del(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.del(key);
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:53:10
     * @Description 刷新
     * @Param []
     * @Return void
     */
    public void flushDB() {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.flushDB();
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:53:19
     * @Description 大小
     * @Param []
     * @Return java.lang.Long
     */
    public Long dbSize() {
        Long dbSize = 0L;
        Jedis jedis = jedisPool.getResource();
        try {
            dbSize = jedis.dbSize();
        } finally {
            jedisPool.returnResource(jedis);
        }
        return dbSize;
    }

    /*
     * @Author 西野
     * @Date 2018-08-17 15:53:34
     * @Description 存值
     * @Param [pattern]
     * @Return java.util.Set<byte[]>
     */
    public Set<byte[]> keys(String pattern) {
        Set<byte[]> keys = null;
        Jedis jedis = jedisPool.getResource();
        try {
            keys = jedis.keys(pattern.getBytes());
        } finally {
            jedisPool.returnResource(jedis);
        }
        return keys;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getExpire() {
        return expire;
    }

    public void setExpire(int expire) {
        this.expire = expire;
    }

    public void destroy() {
        jedisPool.destroy();
    }
}
